﻿using System;
using System.Collections.Generic;
using Microsoft.Live;
using Notes.Helpers;
using System.IO;
using System.Text;

namespace Notes
{
    public class SkyDriveHelper
    {
        public event Action<string> OnFolderIdFound;
        public event Action<string> OnFileIdFound;
        public event Action<string> OnFileContentReady;

        private LiveConnectClient client = null;
        private string FolderName { get; set; }
        private string FolderID { get; set; }
        private string FileName { get; set; }
        private string FileID { get; set; }

        public SkyDriveHelper(LiveConnectClient client)
        {
            this.client = client;
        }

        #region get folder by name

        public void GetFolderIdByName(string name)
        {
            FolderName = name;
            if (client == null || client.Session == null)
            {
                Messenger.ShowMessage("Error", "You must sign in first.");
            }
            else
            {
                client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(GetFolderProperties_Completed);
                client.GetAsync("me/skydrive/files?filter=folders,albums");
            } 
        }

        private void GetFolderProperties_Completed(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Dictionary<string, object> folderData = (Dictionary<string, object>)e.Result;
                List<object> folders = (List<object>)folderData["data"];

                foreach (object item in folders)
                {
                    Dictionary<string, object> folder = (Dictionary<string, object>)item;
                    if (folder["name"].ToString() == FolderName)
                        FolderID = folder["id"].ToString();
                }

                if (FolderID != string.Empty)
                {
                    if (OnFolderIdFound != null)
                        OnFolderIdFound(FolderID);
                }
            }
            else
            {
                Messenger.ShowMessage("Error", e.Error.Message);
            }
        }

        #endregion

        #region get file id by name

        public void GetFileIdByName(string name)
        {
            FileName = name;
            if (client == null || client.Session == null)
            {
                Messenger.ShowMessage("Error", "You must sign in first.");
            }
            else
            {
                client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(GetFileProperties_Completed);
                client.GetAsync("me/skydrive/files");
            }
        }

        private void GetFileProperties_Completed(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Dictionary<string, object> fileData = (Dictionary<string, object>)e.Result;
                List<object> files = (List<object>)fileData["data"];

                foreach (object item in files)
                {
                    Dictionary<string, object> file = (Dictionary<string, object>)item;
                    if (file["name"].ToString() == FileName)
                        FileID = file["id"].ToString();
                }

                if (FileID != string.Empty)
                {
                    if (OnFileIdFound != null)
                        OnFileIdFound(FileID);
                }
            }
            else
            {
                Messenger.ShowMessage("Error", e.Error.Message);
            }
        }

        #endregion

        #region get file content by id

        public void GetFileContentById(string id)
        {
            if (client == null || client.Session == null)
            {
                Messenger.ShowMessage("Error", "You must sign in first.");
            }
            else
            {
                var FileContent = id += "/content";
                client.DownloadAsync(FileContent);
                client.DownloadCompleted +=new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadCompleted);
            }
        }

        void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                StreamReader reader = new StreamReader(e.Result, Encoding.UTF8);
                string xml = reader.ReadToEnd();
                if (OnFileContentReady != null)
                    OnFileContentReady(xml);

            }
        }

        #endregion
    }
}
